﻿using System.Collections.Generic;
using System.Windows;
using MyFloridaHouse.libSvCommonUtilities;
using MyFloridaHouse.libSvOpenDistricts.DataService;
using MyFloridaHouse.libSvOpenDistricts.Districts;
using MyFloridaHouse.ServiceConn.DistrBldrSvcRef;
using MyFloridaHouse.libSvUtilities.Logging;
using System;

namespace MyFloridaHouse.libSvOpenDistricts.ViewModel
{
    public partial class ViewModelBase
    {
        //TODO move GetLayers into its own Layers object? just like LayersDataDictionary
        public void OnServiceDALGetLayersCompletedEventHandler(LayerSTO sto)
        {
            InitializeLayers(sto.layerList);
            isLayersInitializeCompleted = true;
            CheckInitializedComplete();
        }



        //TODO move GetRegionData into its own RegionData object? just like LayersDataDictionary
        public void OnServiceDALGetRegionDataCompleted(RegionDataListSTO sto)
        {
            try
            {
                regionDataList.Clear();
                foreach (var rgnData in sto.regionDataList)
                {
                    regionDataList.Add(rgnData);
                }
            }
            catch (Exception ex)
            {
                Log.HandleInternalError("Problem loading region data : " + ex.Message.ToString());
            }
        
        }
                    

                    



        private void GetDistrictShape(int districtID, string condensedDistrictList)
        {
            serviceDAL.GetDistrictShape(districtID, condensedDistrictList, new System.EventHandler<GetDistrictShapeCompletedEventArgs>(GetDistrictShapeCompleted));

        }

        public void GetDistrictShapeCompleted(object sender, GetDistrictShapeCompletedEventArgs args)
        {
            serviceDAL.getDistrictShapeRequestCount--;
            mapLasso.Hide();
            mapLasso.Reset(getLassoMode());
            if (!ServiceDAL.IsError("GetDistrictShapeCompleted", args.Error, args.Result, args.Result.errorMessage))
            {
                try
                {
                    ReplaceDistrict(args.Result);
                }
                catch (Exception ex)
                {
                    Log.HandleInternalError("Unable to replace district : " + ex.Message.ToString());
                }
            }
        }

        private void GetDistrictDataSummary(int districtID, string condensedDistrictList)
        {
            serviceDAL.GetDistrictDataSummary(districtID
                                             , condensedDistrictList
                                             , dataGridManager.activeLayerIDList
                                             , dataGridManager.activeDictionaryIDList
                                             , new System.EventHandler<GetDistrictDataSummaryCompletedEventArgs>(GetDistrictDataSummaryCompleted));
        }

        public void GetDistrictDataSummaryCompleted(object sender, GetDistrictDataSummaryCompletedEventArgs args)
        {
            serviceDAL.getDistrictDataSummaryRequestCount--;

            if (!ServiceDAL.IsError("GetDistrictDataSummaryCompleted", args.Error, args.Result, args.Result.errorMessage))
            {
                try
                {
                    ReplaceDistrictDataSummary(args.Result);
                }
                catch (Exception ex)
                {
                    Log.HandleInternalError("Unable to replace district summary: " + ex.Message.ToString());
                }
            }
        }


        public void GetShapeUogIDs()
        {
            // turn the lasso shape from the wkt into a list of uogIDs
            serviceDAL.GetShapeUogIDs
                    (mapLasso.GetWKT()
                    , buildLayerList[buildLayerListSelectedIndex].layerID       // get the layer being used as building blocks
                    , districtPlan.selectedDistrict
                    , new System.EventHandler<GetShapeUogIDsCompletedEventArgs>(GetShapeUogIDsCompleted));
        }

        public void GetShapeUogIDsCompleted(object sender, GetShapeUogIDsCompletedEventArgs args)
        {
            mapLasso.Reset(getLassoMode());
            if (!ServiceDAL.IsError("GetShapeUogIDsCompleted", args.Error, args.Result, args.Result.errorMessage))
            {
                try
                {
                    // Everything OK!
                    List<UOG> uogIDList = new List<UOG>();
                    foreach (var uog in args.Result.uogidList)
                    {
                        uogIDList.Add(new UOG(uog.uogID));
                    }

                    if (uogIDList.Count > Constants.MaxDistrictAddShapeCount)
                    {
                        MessageBox.Show("You selected " + uogIDList.Count.ToString() + " shapes to add, please limit selection to no more than " + Constants.MaxDistrictAddShapeCount.ToString() + " shapes.");
                    }
                    else
                    {
                        districtPlan.MoveShapesToNewDistrict(args.Result.districtID, uogIDList);
                    }
                }
                catch (Exception ex)
                {
                    Log.HandleInternalError("Problem reassigning elements to district(s): " + ex.Message.ToString());
                }

            }
        }
    }
}
